home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / BOFH / HQHackMac.m < prev    next >
Encoding:
Text File  |  2001-06-23  |  3.6 KB  |  87 lines

  1. // HQHackMac.m
  2. //
  3. // You may freely copy, distribute, and reuse the code in this example.
  4. // Apple disclaims any warranty of any kind, expressed or  implied, as to its
  5. // fitness for any particular use.
  6.  
  7. #import "HQHackMac.h"
  8. #import "HQInfoPanelController.h"
  9. #import "HQPreferencesController.h"
  10. #import "HQApplication.h"
  11. #import <stdlib.h>
  12.  
  13. BOOL gOn = false;
  14.  
  15. // If you want the bundle to insert a menu into apps that load it, uncomment the next line.  Otherwise, comment it out.
  16. #define ENABLE_MENU
  17.  
  18. @implementation HQHackMac
  19.  
  20. // ************************* Set up *************************
  21.  
  22. + (void)load {
  23.     [HQApplication doNSAppInstancePose];
  24.     srand(time(NULL));
  25. #ifdef ENABLE_MENU
  26.     // Arrange to install the menu.  We will try at least three different ways of getting notified.
  27.     // We try to install the menu at willFinishLaunching, but we will try again at didFinishLaunching just in case willFinishLaunching passed us by.
  28.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryToInstallMenu:) name:NSApplicationWillFinishLaunchingNotification object:NSApp];
  29.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryToInstallMenu:) name:NSApplicationDidFinishLaunchingNotification object:NSApp];
  30.     // Finally, there may be cases where the didFinishLaunching has already happened by the time we get loaded.  So we'll also try it with a timer too.
  31.     [self performSelector:@selector(tryToInstallMenu:) withObject:nil afterDelay:0.0];
  32. #endif
  33. }
  34.  
  35. // ********************** Install extras menu **********************
  36.  
  37. + (void)tryToInstallMenu:(NSNotification *)notification {
  38.     static BOOL alreadyInstalled = NO;
  39.     NSMenu *mainMenu = nil;
  40.  
  41.     // We only try once, but that one time needs to be after there is a main menu.  If there is none now, we will pass and hope this method gets called again later.
  42.     if (!alreadyInstalled && ((mainMenu = [NSApp mainMenu]) != nil)) {
  43.         NSMenu *insertIntoMenu = nil;
  44.         NSMenuItem *item;
  45.         unsigned insertLoc = NSNotFound;
  46.         NSBundle *bundle = [NSBundle bundleForClass:[HQHackMac class]];
  47.         // Succeed or fail, we do not try again.
  48.         alreadyInstalled = YES;
  49.  
  50.         item = (([mainMenu numberOfItems] > 0) ? [mainMenu itemAtIndex:0] : nil);
  51.         if (item && [item hasSubmenu]) {
  52.             insertIntoMenu = [item submenu];
  53.             item = [insertIntoMenu itemWithTitle:NSLocalizedStringFromTableInBundle(@"Services", @"Localizable", bundle, @"Title of Services menu item")];
  54.             if (!item) {
  55.                 // MF: Try again with unlocalized string "Text".  This is a backstop that assumes that any app not localized into the user's chosen language was probably developed in English...
  56.                 item = [insertIntoMenu itemWithTitle:@"Services"];
  57.             }
  58.             if (item) {
  59.                 insertLoc = [[insertIntoMenu itemArray] indexOfObjectIdenticalTo:item] + 1;
  60.             } else {
  61.                 // Just insert at head of menu.  This is kind of yucky...
  62.                 insertLoc = 0;
  63.             }
  64.         } 
  65.  
  66.         if (insertIntoMenu) {
  67.             item = [insertIntoMenu insertItemWithTitle:NSLocalizedStringFromTableInBundle(@"BOFH - OFF", @"Localizable", bundle, @"Title of bofh menu") action:@selector(runBOFH:) keyEquivalent:@"" atIndex:insertLoc];
  68.             [item setTarget:self];
  69.         }
  70.     }
  71. }
  72.  
  73. + (void)runBOFH:(id)sender {
  74.     if (gOn) [sender setTitle:@"BOFH - OFF"];
  75.     else [sender setTitle:@"BOFH - ON"];
  76.     gOn = !gOn;
  77. }
  78.  
  79. + (BOOL)validateMenuItem:(NSMenuItem *)item {
  80.     if ([item action] == @selector(leetAction:)) {
  81.         [item setState:gOn];
  82.     }
  83.     return YES;
  84. }
  85.  
  86. @end
  87.